//
//  HMToolBarView.swift
//  Weibo
//
//  Created by 李学林 on 15/12/10.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit

class HMToolBarView: UIView {
    
    var retweetButton :UIButton?
    var commentButton :UIButton?
    var unlikeButton :UIButton?

    
    var statusViewModel: HMStatusViewModel?{
        didSet{
            // 设置数据
                // 测试
           //printLog(statusViewModel?.status?.reposts_count)
            retweetButton?.setTitle(statusViewModel?.retweetCountStr, forState: .Normal)
            commentButton?.setTitle(statusViewModel?.commentCountStr, forState: .Normal)
            unlikeButton?.setTitle(statusViewModel?.attitudeCountStr, forState: .Normal)
            
        }
    
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        // 设置视图
        backgroundColor = RandomColor()

        // 添加子控件
         retweetButton = addButtons("转发", imageName: "timeline_icon_retweet")
         commentButton = addButtons("评论", imageName: "timeline_icon_comment")
         unlikeButton = addButtons("赞", imageName: "timeline_icon_unlike")
        
        let line1 = addSeparater()
        let line2 = addSeparater()
        
        // 添加约束
        retweetButton?.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
            
        }
        commentButton?.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton!)
        }
            unlikeButton!.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        line1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
        
        line2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
    }
    
    private func addButtons(title: String, imageName: String) -> UIButton {
        
        let button = UIButton()
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Highlighted)
        button.titleLabel?.font =  UIFont.systemFontOfSize(14)
        button.setImage(UIImage(named: imageName), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "timeline_retweet_background"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_retweet_background_highlighted"), forState: .Highlighted)

        button.backgroundColor = UIColor.whiteColor()
        addSubview(button)
        return button
    }
    
    private func addSeparater()->UIView{
        
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
       
        addSubview(imageView)
        
        return imageView
        
    }
    
}
